package com.woniuxy.microsysserver.mapper;

import com.woniuxy.microsysserver.bean.AdminInfo;
import com.woniuxy.microsysserver.bean.OptInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface OptMapper {

    @Insert("insert into opt_info (fk_admin,create_time,opt,ip)" +
            "values (#{p.adminInfo.id},now(),#{p.opt},#{p.ip})")
    void add(@Param("p") OptInfo optInfo);

    @Results({
            @Result(property = "adminInfo",column = "fk_admin",javaType = AdminInfo.class,one =
            @One(select = "com.woniuxy.microsysserver.mapper.AdminMapper.findById",fetchType = FetchType.LAZY))
    })
    @Select("select id,ip,create_time as createTime,opt,fk_admin from opt_info ")
    List<OptInfo> findAll();

    /**
     * 根据事务日志的编号，查询该事务是否已经处理
     * @param txNo
     * @return
     */
    @Select("select count(1) from de_duplication where tx_no = #{txNo}")
    int isExistTx(String txNo);

    /**
     * 添加事务日志
     * @param txNo
     * @return
     */
    @Insert("insert into de_duplication values(#{txNo},now(),1);")
    int addTx(String txNo);

    @Update("update de_duplication set state = 2 where tx_no = #{txNo}")
    void changeTx(@Param("txNo") String txNo);

    @Select("select state from de_duplication where tx_no = #{txNo}")
    int isConsumed(String txNo);
}
